package ${packagePath}${servicePackage};

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import cn.hutool.core.util.StrUtil;
import frame.icloud.base.service.BaseService;
import frame.icloud.base.util.QueryModelReader;
import frame.icloud.common.util.ModelService;
import frame.icloud.global.entity.queryclause.QueryClauseByPage;
import frame.icloud.global.info.GlobalHandler;
import lombok.extern.slf4j.Slf4j;
import ${packagePath}entity.user.TenantUserInfo;
import ${packagePath}util.TenantUtil;
import ${packagePath}${mapperPackage}.${entity}Dao;
import ${packagePath}${entityPackage}.${entity};

/**
 * ${introduce}
 * author: ${author}
 * date: ${date}
 */
@Slf4j
@Service
public class ${entity}Service extends BaseService<${entity}Dao, ${entity}> {

    @Autowired
    private TenantUtil tenantUtil;
    
    /**
     * 自定义分页查询(页码)
     *
     * @param queryClause 查询参数
     * @return List<${entity}>
     */
    public Page<${entity}> getListPageByWhere(QueryClauseByPage queryClause) {
        String sql = "Select " + ModelService.getSelectColumn(${entity}.class, "t") + " from "
            + ModelService.getTableName(${entity}.class) + " t";

        // 查询条件,默认排序
        String where = "";
        String orderByField = "t.CreateDate";
        Map<String, Object> queryInfo =
            new QueryModelReader<${entity}>().getQueryInfo(queryClause, sql, where, orderByField);

        // 分页查询
        Page<${entity}> page = new Page<>(queryClause.getPageIndex(), queryClause.getPageSize());
        page.setRecords(this.baseMapper.getListPageByWhere(page, queryInfo));

        return page;
    }

    /**
     * 获取所有数据
     *
     * @return List<${entity}>
     */
    public List<${entity}> getAllList() {
        // 查询条件
        QueryWrapper<${entity}> qw = new QueryWrapper<>();

        // 查询数据
        return super.list(qw);
    }
    
    /**
     * 根据 主键 获取单条数据
     *
     * @param uuid Uuid
     * @return ${entity}
     */
    public ${entity} getItemByUuid(String uuid) {
        // 查询条件
        QueryWrapper<${entity}> qw = new QueryWrapper<>();
        qw.lambda().eq(${entity}::getUuid, uuid);

        // 查询数据
        return super.getOne(qw);
    }
    
    /**
     * 插入对象
     *
     * @param item 插入对象
     * @return ${entity}
     */
    @Transactional(rollbackFor = Exception.class)
    public ${entity} insert(${entity} item) {
        // 默认数据
        this.setInsertDefault(item);

        // 从redis中获取用户登录信息
        TenantUserInfo tenantUserInfo = tenantUtil.getTenantLoginInfo();
        String tenantId = tenantUserInfo.getTenantId();
        item.setSpaceId(tenantId);

        // 保存
        super.save(item);

        return item;
    }
    
    /**
     * 删除对象
     *
     * @param item 删除对象
     * @return bool
     */
    @Transactional(rollbackFor = Exception.class)
    public Boolean delete(${entity} item) {
        // 删除数据库
        boolean result = super.deleteById(item);

        return result;
    }
    
    /**
     * 更新对象
     *
     * @param item 更新对象
     * @return ${entity}
     */
    @Transactional(rollbackFor = Exception.class)
    public ${entity} update(${entity} item) {
        // 默认数据
        this.setUpdateDefault(item);

        // 保存
        super.updateById(item);

        return item;
    }
    
    /**
     * 设置修改默认值
     *
     * @param item 更新对象
     */
    public void setUpdateDefault(${entity} item) {
        item.setModifyId(GlobalHandler.getUserId());
        item.setModifyDate(new Date());
    }
    
     /**
     * 设置添加默认值
     *
     * @param item 添加对象
     */
    public void setInsertDefault(${entity} item) {
        if (StrUtil.isEmpty(item.getCreateId())) {
            item.setCreateId(GlobalHandler.getUserId());
        }
        item.setCreateDate(new Date());
    }

}
